package com.stylefeng.guns.rest.modular.user;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.api.user.UserApi;
import com.stylefeng.guns.api.user.vo.UserInfoModel;
import com.stylefeng.guns.api.user.vo.UserModel;
import com.stylefeng.guns.core.util.MD5Util;
import com.stylefeng.guns.rest.common.enums.UserValid;
import com.stylefeng.guns.rest.common.persistence.dao.TUserMapper;
import com.stylefeng.guns.rest.common.persistence.model.TUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Service(interfaceClass = UserApi.class, loadbalance = "roundrobin")
public class UserServiceImpl implements UserApi {

    @Autowired
    private TUserMapper userMapper;
    //盐值
    private static final String salt = "xiaofengzi";

    @Override
    public long login(String userName, String password) {
        TUser tUser = new TUser();
        tUser.setUserName(userName);
        TUser result = userMapper.selectOne(tUser);
        if(result!=null && result.getId()>0 && result.getValid().intValue()==UserValid.CAN_USE.getStatus()){
            //判断密码
            if(result.getPassword().equals(MD5Util.encrypt(result.getUserName()+salt+password))){
                return result.getId();
            }
        }
        return 0L;
    }

    @Override
    public boolean register(UserModel userModel) {
        TUser tUser = new TUser();
        tUser.setUserName(userModel.getUserName());
        //用户名+盐值+密码 进行加密
        tUser.setPassword(MD5Util.encrypt(userModel.getUserName()+salt+userModel.getPassword()));
        tUser.setNickName(userModel.getNickName());
        tUser.setEmail(userModel.getEmail());
        tUser.setPhone(userModel.getPhone());
        tUser.setValid(UserValid.CAN_USE.getStatus());
        Integer insert = userMapper.insert(tUser);
        if(insert>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean checkUserName(String userName) {
        EntityWrapper<TUser> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("user_name", userName);
        entityWrapper.eq("valid", UserValid.CAN_USE.getStatus());
        Integer result = userMapper.selectCount(entityWrapper);
        if(result!=null && result>0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 将do装换成vo
     * @param tUser
     * @return
     */
    private UserInfoModel do2UserInfo(TUser tUser) {
        UserInfoModel userInfoModel = new UserInfoModel();
        userInfoModel.setUserId(tUser.getId());
        userInfoModel.setUserName(tUser.getUserName());
        userInfoModel.setNickName(tUser.getNickName());
        userInfoModel.setEmail(tUser.getEmail());
        userInfoModel.setPhone(tUser.getPhone());
        userInfoModel.setCreateTime(tUser.getCreateTime().getTime());
        userInfoModel.setUpdateTime(tUser.getUpdateTime().getTime());
        userInfoModel.setValid(tUser.getValid());
        return userInfoModel;
    }

    @Override
    public UserInfoModel getUserInfo(long id) {
        TUser result = userMapper.selectById(id);
        UserInfoModel userInfoModel = do2UserInfo(result);
        return userInfoModel;
    }

    @Override
    public UserInfoModel updateUserInfo(UserInfoModel userInfoModel) {
        TUser tUser = new TUser();
        tUser.setId(userInfoModel.getUserId());
        tUser.setUserName(userInfoModel.getUserName());
        tUser.setNickName(userInfoModel.getNickName());
        tUser.setEmail(userInfoModel.getEmail());
        tUser.setPhone(userInfoModel.getPhone());
        tUser.setUpdateTime(new Date(System.currentTimeMillis()));
        Integer result = userMapper.updateById(tUser);
        if(result!=null && result>0){
            UserInfoModel userInfo = getUserInfo(userInfoModel.getUserId());
            return userInfo;
        }
        return userInfoModel;
    }
}
